<?php

namespace humhub\modules\api\controllers;

use Yii;
use humhub\modules\like\models\Like;
use humhub\modules\user\widgets\UserListBox;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;

/**
 * 赞、取消赞
 */
class LikeController extends Base3Controller
{

    /**
     * @inheritdoc
     */
    public function init(){
        if( Yii::$app->request->post('model')=='post' ){
            $_GET['contentModel'] = 'humhub\modules\post\models\Post';
            $_POST['contentModel'] = 'humhub\modules\post\models\Post';
        }
        if( Yii::$app->request->post('model')=='comment' ){
            $_GET['contentModel'] = 'humhub\modules\comment\models\Comment';
            $_POST['contentModel'] = 'humhub\modules\comment\models\Comment';
        }
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        $behaviors['acl'] = [
            'class' => \humhub\components\behaviors\AccessControl::className(),
            'guestAllowedActions' => ['show-likes']
        ];

        return $behaviors;
    }

    /**
     * @inheritdoc
     */
    public function actions(){
        $actions = parent::actions();
        unset($actions['update'], $actions['create'], $actions['index'], $actions['delete']);
        return $actions;
    }

    /**
     * 赞
     *
     * POST /api/like/like
     *
     * post.contentId 内容ID
     *
     * post.model 内容所属类型，=post内容， =comment评论
     *
     * @return boolean
     */
    public function actionLike(){
        $_GET['contentId'] = Yii::$app->request->post('contentId');
        $this->forcePostRequest();

        $like = Like::findOne(['object_model' => $this->contentModel, 'object_id' => $this->contentId, 'created_by' => Yii::$app->user->id]);
        if ($like === null) {

            // Create Like Object
            $like = new Like();
            $like->object_model = $this->contentModel;
            $like->object_id = $this->contentId;
            $like->save();
        }

        return $this->actionShowLikes();
    }

    /**
     * 取消赞 
     *
     * POST /api/like/unlike
     *
     * post.contentId 内容ID
     *
     * post.model 内容所属类型，=post内容， =comment评论
     *
     * @return boolean
     */
    public function actionUnlike(){
        $_GET['contentId'] = Yii::$app->request->post('contentId');
        $this->forcePostRequest();

        if (!Yii::$app->user->isGuest) {
            $like = Like::findOne(['object_model' => $this->contentModel, 'object_id' => $this->contentId, 'created_by' => Yii::$app->user->id]);
            if ($like !== null) {
                $like->delete();
            }
        }

        return $this->actionShowLikes(false);
    }

    /**
     * @inheritdoc
     */
    public function actionShowLikes($like=true){
        Yii::$app->response->format = 'json';

        // Some Meta Infos
        $currentUserLiked = false;

        $likes = Like::GetLikes($this->contentModel, $this->contentId);

        foreach ($likes as $one) {
            if ($one->user->id == Yii::$app->user->id) {
                $currentUserLiked = true;
            }
        }

        if( $like ){
            //赞
            if( $currentUserLiked===false ){
                Yii::$app->params['res-success'] = false;
            }
            return $currentUserLiked;
        }else{
            //$currentUserLiked = !$currentUserLiked;
            //取消赞
            if( $currentUserLiked===true ){
                Yii::$app->params['res-success'] = false;
                return false;
            }
            return true;
        }

        return [
            'currentUserLiked' => $currentUserLiked,
            'likeCounter' => count($likes)
        ];
    }
}
